import Vue from 'vue'
import { Loading } from 'element-ui';

import { getButtons } from "@/api/system/permission";//按钮权限

/**
 * 按钮权限
 * By：苏恩泽
 */
var button_list=null;
var url = null;

//初始化按钮
export function permConfig(path){//
		if(path.indexOf('/')==0) path=path.substring(1,path.length);
		url=path;
}

//获取节点
export const bntDiv = Vue.directive('bntDiv', {
	bind: function (el, binding, vnode) {
		var bntList = el.children;
		//console.log(el.childNodes.length+" - "+bntList.length);
		
		for (var i = 0; i < bntList.length; i++) {
			bntList[i].style.display ="none";
		}
		
		
		getButtons(url).then(response => {
				var rlist = response.data;
				button_list=[];
				for (var i = 0; i < rlist.length; i++) {
					button_list.push(rlist[i].name)
				}
				
				for (var i = 0; i < bntList.length; i++) {
					var code = bntList[i].getAttribute("perm");
					if(button_list.indexOf(code)!=-1||!code){
						bntList[i].style.display ="inline";
					}else{
						//el.removeChild(bntList[i]);
					}
				} 
				console.log(el);
		});	
	},
	unbind: function (el, binding, vnode) {
			button_list = null;
			url = null;
	},
});



//export {bntPerm}

/***
用法:
引入：
import {bntPerm}  from "@/utils/btnPermissions.js";//权限验证工具
data添加：
moduleName:'company_index',//模块名称，处理权限用的
按钮添加
v-bntPerm="{moduleName:moduleName,code:'AAA'}"
***/